<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring MVC 并发处理与线程安全指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 8px;
            right: 12px;
            font-size: 12px;
            color: #888;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Spring MVC 并发处理与线程安全
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    构建高性能、稳定的 Web 应用程序的核心技术指南
                </p>
                <div class="flex justify-center space-x-4">
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>线程安全</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-tachometer-alt mr-2"></i>
                        <span>高性能</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-code mr-2"></i>
                        <span>最佳实践</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="content-card rounded-xl shadow-lg p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>Spring MVC 中处理并发请求并确保线程安全是构建高性能、稳定的 Web 应用程序的关键。本指南将深入探讨 Spring MVC 的并发处理机制，并提供确保线程安全的最佳实践。
                </p>
            </div>

            <!-- Section 1: 并发请求处理机制 -->
            <div class="content-card rounded-xl shadow-lg p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    1. Spring MVC 处理并发请求的机制
                </h2>
                
                <div class="grid md:grid-cols-3 gap-6 mb-8">
                    <div class="text-center">
                        <div class="feature-icon mx-auto">
                            <i class="fas fa-network-wired"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">请求分发</h3>
                        <p class="text-gray-600">基于 Servlet 容器的多线程处理模型</p>
                    </div>
                    <div class="text-center">
                        <div class="feature-icon mx-auto">
                            <i class="fas fa-route"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">DispatcherServlet</h3>
                        <p class="text-gray-600">核心组件负责请求路由分发</p>
                    </div>
                    <div class="text-center">
                        <div class="feature-icon mx-auto">
                            <i class="fas fa-cogs"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">Controller</h3>
                        <p class="text-gray-600">单例模式的请求处理器</p>
                    </div>
                </div>

                <div class="space-y-6">
                    <div>
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-server mr-3 text-purple-600"></i>
                            1.1 请求分发
                        </h4>
                        <p class="text-gray-700 leading-relaxed pl-9">
                            Spring MVC 是基于 Servlet 的，Servlet 容器（如 Tomcat）负责处理 HTTP 请求的并发。每个请求都由容器分配一个新的线程来处理。Spring MVC 控制器和服务层代码都是线程安全的，只要这些组件本身是设计为线程安全的。
                        </p>
                    </div>

                    <div>
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-directions mr-3 text-purple-600"></i>
                            1.2 DispatcherServlet
                        </h4>
                        <p class="text-gray-700 leading-relaxed pl-9">
                            <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">DispatcherServlet</code> 是 Spring MVC 的核心组件，它负责将请求分发到具体的处理器（即控制器）。<code class="bg-gray-100 px-2 py-1 rounded text-purple-600">DispatcherServlet</code> 是线程安全的，它的工作是将请求分发到控制器和视图解析器，但它不持有任何请求特定的数据，因此不需要考虑线程安全问题。
                        </p>
                    </div>

                    <div>
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-code-branch mr-3 text-purple-600"></i>
                            1.3 Controller 和 Handler
                        </h4>
                        <p class="text-gray-700 leading-relaxed pl-9 mb-4">
                            在 Spring MVC 中，控制器（<code class="bg-gray-100 px-2 py-1 rounded text-purple-600">@Controller</code> 或 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">@RestController</code> 注解的类）通常是单例的。Spring 框架通过控制器的实例来处理请求，因此控制器实例本身必须是线程安全的。这意味着：
                        </p>
                        <ul class="list-disc list-inside space-y-2 pl-9 text-gray-700">
                            <li>控制器的字段和状态不能是线程不安全的。</li>
                            <li>避免在控制器中存储请求特定的状态。</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Architecture Diagram -->
            <div class="content-card rounded-xl shadow-lg p-8 mb-8">
                <h3 class="text-2xl font-bold mb-6 text-center text-gray-800">Spring MVC 请求处理架构</h3>
                <div class="mermaid">
                    graph TB
                        A[客户端请求] --> B[Servlet容器]
                        B --> C[线程池]
                        C --> D[DispatcherServlet]
                        D --> E[HandlerMapping]
                        E --> F[Controller]
                        F --> G[Service层]
                        G --> H[Repository层]
                        H --> I[数据库]
                        F --> J[ViewResolver]
                        J --> K[响应返回]
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style D fill:#bbf,stroke:#333,stroke-width:2px
                        style F fill:#bfb,stroke:#333,stroke-width:2px
                </div>
            </div>

            <!-- Section 2: 线程安全策略 -->
            <div class="content-card rounded-xl shadow-lg p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    2. 确保线程安全的策略
                </h2>

                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-lg">
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-lock mr-3 text-purple-600"></i>
                            2.1 无状态设计
                        </h4>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-green-500"></i>
                                <div>
                                    <strong>无状态控制器：</strong>控制器的设计应为无状态的，即控制器不应持有任何请求特定的状态。
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-green-500"></i>
                                <div>
                                    <strong>避免共享数据：</strong>控制器中的字段应尽量避免存储请求级别的数据。
                                </div>
                            </li>
                        </ul>
                    </div>

                    <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-lg">
                        <h4 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-layer-group mr-3 text-blue-600"></i>
                            2.2 线程安全的服务层
                        </h4>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-green-500"></i>
                                <div>
                                    <strong>服务层设计：</strong>服务层（<code class="bg-white px-2 py-1 rounded">@Service</code>）也通常是单例的。
                                </div>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle mr-2 mt-1 text-green-500"></i>
                                <div>
                                    <strong>使用同步机制：</strong>必要时使用 <code class="bg-white px-2 py-1 rounded">synchronized</code> 或并发工具类。
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>

                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-gradient-to-br from-green-50 to-teal-50 p-6 rounded-lg">